package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.object.pojo.dto.ClubRecordDTO;
import com.tbit.uqbike.object.pojo.dto.GiveClubCardDTO;
import com.tbit.uqbike.object.pojo.dto.ImportUserDTO;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.uqbike.object.pojo.vo.GiveRideCardVo;
import com.tbit.uqbike.object.pojo.vo.VipGiveLog;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.BatchGiveClubCardService;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.tbit.uqbike.webmanager.controller.BaseController.getLogin;

/**
 * 批量赠送会员卡
 *
 * @Author lhx
 * @ClassName BatchGiveClubCardController
 * @Description
 * @date 2023/3/21 11:06
 * @Version 1.0
 */
@RestController
@RequestMapping("/batchGiveClubCard")
public class BatchGiveClubCardController {

    @Autowired
    private BatchGiveClubCardService batchGiveClubCardService;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 批量赠送会员卡
     *
     * @param giveClubCardDTO
     * @return
     */
    @PostMapping("/giveClubCard")
    public Result<List<GiveRideCardVo>> giveClubCard(@RequestParam("file") MultipartFile file, GiveClubCardDTO giveClubCardDTO,
                                                     HttpServletRequest request) {

        LoginInfo login = getLogin(request);
        /**操作密码校验*/
        operaPswService.checkOperaPsw(login, OperaPswEnum.OPERA_PSW_COUPONS_CONFIG.getModule(), giveClubCardDTO.getPsw());

        return batchGiveClubCardService.giveClubCard(giveClubCardDTO, login, file);
    }

    /**
     * 分页查询赠送会员卡记录
     *
     * @param clubRecordDTO
     * @return
     */
    @GetMapping("/queryPageClubCardRecord")
    public Result<PageResult<VipGiveLog>> queryPageClubCardRecord(ClubRecordDTO clubRecordDTO, HttpServletRequest request) {
        LoginInfo login = getLogin(request);
        if (login.getType() != 0 && clubRecordDTO.getBrandId() == null) {
            return Result.error("请选择品牌");
        }
        PageResult<VipGiveLog> pageResult = batchGiveClubCardService.queryPageClubRecord(clubRecordDTO);
        return Result.success(pageResult);
    }

    /**
     * 会员卡赠送记录导出
     *
     * @param clubRecordDTO
     * @return
     */
    @GetMapping("/exportClubCardRecord")
    public Result exportClubCardRecord(ClubRecordDTO clubRecordDTO) {
        return batchGiveClubCardService.exportClubCardRecord(clubRecordDTO);
    }

    /**
     * 批量新增用户
     *
     * @return
     * @Param importUserDTOList
     */
    @PostMapping("/importUser")
    public Result<BatchOperationMsgVO<String>> importUser(ImportUserDTO importUserDTO) {
        /**操作密码校验*/
        operaPswService.checkOperaPsw(getLogin(ServletUtils.getRequest()), OperaPswEnum.OPERA_PSW_OPERATION_CONFIG.getModule(),
                    importUserDTO.getPsw());

        return batchGiveClubCardService.importUser(importUserDTO);
    }

}
